package com.xmall.auth.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmall.auth.entity.SysRoleMenuRelation;
import com.xmall.auth.service.ISysRoleMenuRelationService;
import com.xmall.common.config.CommonPage;
import com.xmall.common.config.CommonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 后台角色菜单关系表 前端控制器
 * </p>
 *
 * @author liuzh
 * @since 2020-06-14
 */
@RestController
@RequestMapping("/sys-role-menu-relation" )
public class SysRoleMenuRelationController {
    @Autowired
    private ISysRoleMenuRelationService sysRoleMenuRelationService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult registerMenu(@RequestBody SysRoleMenuRelation sysRoleMenuRelation) {
        boolean save = sysRoleMenuRelationService.saveSysRoleMenuRelation(sysRoleMenuRelation);
        if (!save) {
            return CommonResult.failed();
        }
        return CommonResult.success(save);
    }


    @ApiOperation("删除后台角色菜单关系表" )
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public CommonResult delete(@PathVariable Long id) {
        boolean delete = sysRoleMenuRelationService.deleteSysRoleMenuRelation(id);
        if (!delete) {
            return CommonResult.failed();
        }
        return CommonResult.success(delete);
    }

    @ApiOperation("更新后台角色菜单关系表" )
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody SysRoleMenuRelation sysRoleMenuRelation) {
        boolean update = sysRoleMenuRelationService.updateSysRoleMenuRelation(id, sysRoleMenuRelation);
        if (!update) {
            return CommonResult.failed();
        }
        return CommonResult.success(update);
    }

    @ApiOperation("获取后台角色菜单关系表ById" )
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<SysRoleMenuRelation> getSysRoleMenuRelation(@PathVariable Long id) {
        SysRoleMenuRelation sysRoleMenuRelation = sysRoleMenuRelationService.getSysRoleMenuRelation(id);
        return CommonResult.success(sysRoleMenuRelation);
    }


    @ApiOperation("分页查询后台角色菜单关系表" )
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<SysRoleMenuRelation>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                              @RequestParam(value = "pageSize", defaultValue = "5" ) Integer pageSize,
                                                              @RequestParam(value = "pageNum", defaultValue = "1" ) Integer pageNum) {

        Page<SysRoleMenuRelation> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysRoleMenuRelation> queryWrapper = new QueryWrapper<>();
        if (!StrUtil.isEmpty(keyword)) {
            queryWrapper.like("user_name", keyword);
        }
        IPage<SysRoleMenuRelation> pageList = sysRoleMenuRelationService.page(page, queryWrapper);
        return CommonResult.success(CommonPage.restPage(pageList));
    }
}

